<template>
  <!-- 注册框（电话号和短信验证码） -->
  <div class="container" id="login-box" :class="jump ? 'right-panel-active' : ''">
    <div class="form-container sign-up-container">
      <form name="enroll_form">
        <h1>注册</h1>
        <div class="txtb" id="reg_div">
          <input id="reg_input" class="reginster_input" v-model="user.userTel" @blur="numberreg" type="text"
            name="userTel" @focus="up" :class="bool ? 'focus' : ''" />

          <span data-placeholder="Useranme" id="test_uid"></span>
          <button class="btn" @click="sendcode" id="reg_btn">
            获取短信验证码
          </button>
        </div>

        <div class="txtb">
          <input v-model="user.authCode" type="password" name="authCode" @focus="registerup"
            :class="registerbool ? 'focus' : ''" />
          <span data-placeholder="phone" id="test_pwd"></span>
        </div>
        <button type="submit" @click="register">注册</button>
      </form>
    </div>
    <!-- 登录(电话号和密码) -->
    <div class="form-container sign-in-container">
      <form name="login_form">
        <div v-if="pwd">
          <h1>
            登录<button class="anotherlogin" @click="another">
              验证码登录
            </button>
          </h1>
          <div class="txtb">
            <input v-model="users.userTel" type="text" name="user.userTel" @blur="numberreg" @focus="numberup"
              :class="numberbool ? 'focus' : ''" />
            <span data-placeholder="phone number"></span>
          </div>
          <div class="txtb">
            <input v-model="users.userPaw" type="password" name="password" @focus="codeup"
              :class="codebool ? 'focus' : ''" />
            <span data-placeholder="password"></span>
          </div>

          <button type="submit" @click="login">登录</button>
        </div>

        <!-- 另一种登录（验证码登录） -->
        <div v-else>
          <h1>
            登录<button class="anotherlogin" @click="another">
              密码 &nbsp;&nbsp;登录
            </button>
          </h1>
          <div class="txtb" id="short_div">
            <input v-model="userinfo.userTel" type="text" name="user.userTel" @blur="numberreg" @focus="numberup"
              :class="numberbool ? 'focus' : ''" id="short_input" />
            <span data-placeholder="phone number"></span>
            <button class="btn" @click="send" id="short_btn">
              获取短信验证码
            </button>
          </div>

          <div class="txtb">
            <input v-model="userinfo.authCode" type="password" name="password" @focus="codeup"
              :class="codebool ? 'focus' : ''" />
            <span data-placeholder="verification code"></span>
          </div>

          <button type="submit" @click="login_code">登录</button>
        </div>
      </form>
    </div>

    <div class="overlay-container">
      <div class="overlay">
        <div class="overlay-panel overlay-left">
          <h1>已有账号？</h1>
          <p>请使用您的账号进行登录</p>
          <button class="ghost" id="signIn" @click="change">登录</button>
        </div>
        <div class="overlay-panel overlay-right">
          <h1>没有账号?</h1>
          <p>立即注册加入我们，和我们一起开始旅程吧</p>
          <button class="ghost" id="signUp" type="submit" @click="change">
            注册
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Stores from "@/utils/store.js"
import { TOKEN_key } from "@/utils/constant.js"
import { createNamespacedHelpers } from "vuex";
const { mapMutations: mapuserinfomutation } =
  createNamespacedHelpers("userinfo");
export default {
  data() {
    return {
      user: {
        userTel: "",
        authCode: "",
      },
      users: {
        userTel: "",
        userPaw: "",
      },
      userinfo: {
        userTel: "",
        authCode: "",
      },
      pwd: true,
      bool: false,
      registerbool: false,
      jump: false,
      numberbool: false,
      codebool: false,
      // userTel: "111111111",
      // 电话号的正则表达式
      reg: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
    };
  },
  computed: {},
  methods: {
    ...mapuserinfomutation(["getuserinfolist"]),
    up() {
      this.bool = true;
    },
    registerup() {
      this.registerbool = true;
    },
    change() {
      this.jump = !this.jump;
    },
    numberup() {
      this.numberbool = true;
    },
    codeup() {
      this.codebool = true;
    },
    numberreg() {
      // if (this.user_tel.match(this.reg)) {
      // alert("手机号通过了正则");
      // } else {
      // alert("手机号输入有误");
      // }
    },
    async login(e) {
      e.preventDefault();
      console.log(2);
      console.log(this.users);
      // const result = await this.$api.user.login(this.users);
      const result = await this.$api.user.login(this.$qs.stringify(this.users));
      console.log("登录返回值", result);
      console.log("确切值", result.data.user.user);
      // 存储用户信息
      this.getuserinfolist(result.data.user.user);
      if (result.code == 200) {
        Stores.setStore(TOKEN_key, result.data.token)
        this.$router.replace("/main");
      }
      console.log(111111);
      // console.log(this.$router);
    },
    async login_code(e) {
      e.preventDefault();
      // const result = await this.$api.user.login(this.userinfo);
      const result = await this.$api.user.login(this.$qs.stringify(this.userinfo));
      console.log(result);
      if (result.code == 200) {
        Stores.setStore(TOKEN_key)
        this.$router.replace("/main");
      }
    },
    async register(e) {
      e.preventDefault();
      console.log(this.user);
      const result1 = await this.$api.user.register(this.user);
      console.log(result1);
      if (result1.code == 200) {
        this.$message.success(result1.message);
      }
    },
    async sendcode(e) {
      console.log();
      e.preventDefault();
      const info = await this.$api.user.getcode(this.user.userTel);
      console.log(info);
    },
    // 用验证码的登录
    async send(e) {
      e.preventDefault();
      const info = await this.$api.user.getcode(this.userinfo.userTel);
      console.log(info);
    },
    another(e) {
      e.preventDefault();
      this.pwd = !this.pwd;

    },
  },
};
</script>

<style scoped lang="scss">
/* 登录框样式 */
.anotherlogin {
  margin-left: 2.6042vw;
}

h1 {
  font-weight: bold;
  margin: 0;
}

p {
  font-size: 14px;
  line-height: 20px;
  letter-spacing: 0.5px;
  margin: 20px 0 30px;
}

span {
  font-size: 12px;
}

a {
  color: #333;
  font-size: 14px;
  text-decoration: none;
  margin: 15px 0;
}

input {
  background-color: #fff !important;
}

#short_div {
  display: flex;
}

#short_input {
  display: inline-block;
  width: 46%;
}

#short_btn {
  display: inline-block;
  width: 7.2917vw;
  padding: 0 !important;
  background: white;
  color: #333;
  margin: 0;
}

#reg_div {
  display: flex;
}

#reg_input {
  display: inline-block;
  width: 45%;
}

#reg_btn {
  display: inline-block;
  width: 7.5vw;
  padding: 0 !important;
  background: white;
  color: #333;
  margin: 0;
}

.container {
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  position: relative;
  overflow: hidden;
  width: 768px;
  max-width: 100%;
  min-height: 480px;
}

.form-container form {
  background: #fff;
  display: flex;
  flex-direction: column;
  padding: 0 50px;
  height: 100%;
  justify-content: center;
  text-align: center;
}

.social-container {
  margin: 20px 0;
}

.social-container a {
  border: 1px solid #ddd;
  border-radius: 50%;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  margin: 0 5px;
  height: 40px;
  width: 40px;
}

.social-container a:hover {
  background-color: #eee;
}

.txtb {
  border-bottom: 2px solid #adadad;
  position: relative;
  margin: 0.5208vw 0;
}

.txtb input {
  font-size: 15px;
  color: #333;
  border: none;
  width: 100%;
  outline: none;
  background-color: none;
  padding: 0 0.1563vw;
  height: 1.8229vw;
}

.txtb span::before {
  content: attr(data-placeholder);
  position: absolute;
  top: 50%;
  left: 0.2604vw;
  color: #adadad;
  transform: translateY(-50%);
  transition: 0.5s;
}

.txtb span::after {
  content: "";
  position: absolute;
  left: 0%;
  top: 100%;
  width: 0%;
  height: 2px;
  background-image: linear-gradient(120deg, #3498db, #8e44ad);
  transition: 0.5s;
}

.focus {
  background-color: #fff;
}

.focus+span::before {
  top: -5px;
}

.focus+span::after {
  width: 100%;
}

button {
  border-radius: 20px;
  border: 1px solid #ff4b2b;
  background: #ff4b2b;
  color: #fff;
  font-size: 12px;
  font-weight: bold;
  padding: 0.625vw 2.3438vw;
  letter-spacing: 1px;
  text-transform: uppercase;
  transition: transform 80ms ease-in;
  cursor: pointer;
}

button:active {
  transform: scale(0.95);
}

button:focus {
  outline: none;
}

button.ghost {
  background: transparent;
  border-color: #fff;
}

.form-container {
  position: absolute;
  top: 0;
  height: 100%;
  transition: all 0.6s ease-in-out;
}

.form-container button {
  background: linear-gradient(120deg, #c4ecfb, #3366ff);
  border: none;
  background-size: 200%;
  color: #fff;
  transition: 0.5s;
}

.btn {
  background: linear-gradient(120deg, #c4ecfb, #3366ff);
  border: none;
  background-size: 200%;
  width: 10.5vw;
  color: #fff;
  transition: 0.5s;
  margin-left: 6vw;
}

.form-container button:hover {
  background-position: right;
}

.sign-in-container {
  left: 0;
  width: 50%;
  z-index: 2;
}

.sign-in-container form a {
  position: relative;
  left: 5.2083vw;
}

.sign-up-container {
  left: 0;
  width: 50%;
  z-index: 1;
  opacity: 0;
}

.sign-up-container button {
  margin-top: 1.0417vw;
}

.overlay-container {
  position: absolute;
  top: 0;
  left: 50%;
  width: 50%;
  height: 100%;
  overflow: hidden;
  transition: transform 0.6s ease-in-out;
  z-index: 100;
}

.overlay {
  background-image: linear-gradient(120deg, #e0f1fa, #072d94);
  color: #fff;
  position: relative;
  left: -100%;
  height: 100%;
  width: 200%;
  transform: translateY(0);
  transition: transform 0.6s ease-in-out;
}

.overlay-panel {
  position: absolute;
  top: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 0 40px;
  height: 100%;
  width: 50%;
  text-align: center;
  transform: translateY(0);
  transition: transform 0.6s ease-in-out;
}

.overlay-right {
  right: 0;
  transform: translateY(0);
}

.overlay-left {
  transform: translateY(-20%);
}

.container.right-panel-active .sign-in-container {
  transform: translateY(100%);
}

.container.container.right-panel-active .overlay-container {
  transform: translateX(-100%);
}

.container.right-panel-active .sign-up-container {
  transform: translateX(100%);
  opacity: 1;
  z-index: 5;
}

.container.container.right-panel-active .overlay {
  transform: translateX(50%);
}

.container.container.right-panel-active .overlay-left {
  transform: translateY(0);
}

.container.container.right-panel-active .overlay-right {
  transform: translateY(20%);
}
</style>